package com.honeycomb.action;

import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.honeycomb.common.logger.HoneyCombLogger;
import com.honeycomb.common.logger.HoneyCombLoggerFactory;
import com.honeycomb.utils.SystemConstants;
import com.opensymphony.xwork2.ActionSupport;

public abstract class BaseAction extends ActionSupport {

	protected HoneyCombLogger logger = HoneyCombLoggerFactory.getLogger(this.getClass());
	
	protected boolean isLoginNeed() {
		String rqURL = this.getRQ().getRequestURI();
		logger.info("--------requestURL---------"+rqURL);
		String ss = SystemConstants.Login_URL_Validation;
		String[] ssA = ss.split(";");
		return 	Arrays.binarySearch(ssA, rqURL.substring(rqURL.lastIndexOf("/")>=0? rqURL.lastIndexOf("/"):0)) < 0;
	}
	
	protected boolean loginCheck(){
		return !isLoginNeed() ||
			this.getSSN().getAttribute("user") != null;
	}
	
	protected HttpServletRequest getRQ(){
		return ServletActionContext.getRequest();
	}
	
	protected HttpServletResponse getRP(){
		return ServletActionContext.getResponse();
	}
	
	protected HttpSession getSSN(){
		return ServletActionContext.getRequest().getSession();
	}
	
	protected int getPageNum(){
		int pg = 1;
		String pgS = this.getRQ().getParameter("page");
		try{
			pg = Integer.parseInt(pgS);
		}catch(Exception ex){}
		if(pg <= 0){
			pg = 1;
		}
		return pg;
	}

}
